package com.kk.rbac.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kk.rbac.Vo.Meta;
import com.kk.rbac.Vo.Router;
import com.kk.rbac.common.R;
import com.kk.rbac.entity.Menu;
import com.kk.rbac.entity.RoleMenu;
import com.kk.rbac.entity.UserRole;
import com.kk.rbac.mapper.*;
import com.kk.rbac.service.MenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 权限 服务实现类
 * </p>
 *
 * @author kk
 * @since 2020-11-04
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {
    @Resource
    MenuMapper menuMapper;
    @Resource
    UserRoleMapper userRoleMapper;

    @Override
    public List<Router> getRouterByUser() {
        //测试使用id为1的用户
        //查询该用户对应的角色
        List<UserRole> roles = userRoleMapper.selectList(new QueryWrapper<UserRole>().eq("user_id", 1).select("role_id"));
        //查询角色对应的所有路由
        HashSet<Menu> menuSet=new HashSet<>();
        for(UserRole userRole: roles){
            //根据角色获取菜单
            List<Menu> menus=menuMapper.selectMenuByRoleId(userRole.getRoleId());
            menuSet.addAll(menus);
        }
        //构建路由
        List<Router> routers = bulidRouters(menuSet);
        return routers;
    }

    private List<Router> bulidRouters(HashSet<Menu> menuSet) {
        List<Router> routers=new ArrayList<>();
        for (Menu menu: menuSet){
            if (menu.getPid()==1){
                //转换成路由
                Router router = createRouter(menu);
                router.setLevel(1);
                routers.add(findRouterChildren(router,menuSet));
            }
        }
        Collections.sort(routers, (o1,o2)-> o1.getId()-o2.getId());
        return routers;
    }

    /**
     * DFS 添加子路由
     * @param router 父路由
     * @param menuSet 所有路由
     * @return
     */
    private Router findRouterChildren(Router router, HashSet<Menu> menuSet) {
        router.setChildren(new ArrayList<Router>());
        //递归添加子路由
        for(Menu menu:menuSet){
            if (router.getId()==menu.getPid()){
                //若发现子路由
                Router routerSon = createRouter(menu);
                //把等级加1
                int level=router.getLevel()+1;
                routerSon.setLevel(level);
                if (router.getChildren()==null){
                    router.setChildren(new ArrayList<Router>());
                }
                router.getChildren().add(findRouterChildren(routerSon,menuSet));
                Collections.sort(router.getChildren(), (o1,o2)-> o1.getId()-o2.getId());
            }
        }
        return router;
    }

    //构建路由
    private Router createRouter(Menu menu) {
        Router router=new Router();
        router.setId(menu.getId());
        router.setName(menu.getLabel());
        router.setPath(menu.getUrl());
        router.setComponent(menu.getComponent());
        router.setMeta(new Meta().setIcon(menu.getIcon()).setTitle(menu.getPermissionValue()));
        return router;
    }
}
